/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.business.service.impl;

import lombok.extern.slf4j.Slf4j;
import me.zhengjie.business.domain.ZhsqCy;
import me.zhengjie.business.domain.vo.GeneralQueryZhgVo;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.business.repository.ZhsqCyRepository;
import me.zhengjie.business.service.ZhsqCyService;
import me.zhengjie.business.service.dto.ZhsqCyDto;
import me.zhengjie.business.service.dto.ZhsqCyQueryCriteria;
import me.zhengjie.business.service.mapstruct.ZhsqCyMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author xiahongbo
* @date 2024-05-06
**/
@Slf4j
@Service
@RequiredArgsConstructor
public class ZhsqCyServiceImpl implements ZhsqCyService {

    private final ZhsqCyRepository zhsqCyRepository;
    private final ZhsqCyMapper zhsqCyMapper;

    @PersistenceContext
    private EntityManager em;

    @Override
    public Object queryZhsqCy(ZhsqCyQueryCriteria criteria, int[] startEnd) {
        String tempSql =  "select r1.bis_zhsq_id, " +
                "                               r1.bis_zhsq_hljg_id, " +
                "                               r1.bis_zhsq_snry_id, " +
                "                               r1.bis_zhsq_snry_xm, " +
                "                               r1.bis_zhsq_snry_xb, " +
                "                               r1.bis_zhsq_snry_sfz, " +
                "                               r1.bis_zhsq_snry_ybssqx bis_zhsq_snry_ybssqx_code, " +
                "                               (select t3.label from sys_dict_detail t3 where t3.dict_id=1001 and t3.value=r1.bis_zhsq_snry_ybssqx) bis_zhsq_snry_ybssqx_name, " +
                "                               to_char(r1.bis_zhsq_dykkrq, 'yyyy-mm-dd') bis_zhsq_dykkrq, " +
                "                               to_char(r1.bis_zhsq_dyjsrq, 'yyyy-mm-dd') bis_zhsq_dyjsrq, " +
                "                               r1.bis_zhsq_xsdyq, " +
                "                               r1.bis_zhsq_adl, " +
                "                               r1.bis_zhsq_zt, " +
                "                               r1.bis_zhsq_adl_pc, " +
                "                               r1.bis_zhsq_sndj, " +
                "                               r1.bis_zhsq_rybh, " +
                "                               r1.bis_zhsq_nl, " +
                "                               r1.bis_zhsq_lxdh, " +
                "                               r1.bis_zhsq_hldz_ssqx bis_zhsq_hldz_ssqx_code, " +
                "                               (select t3.label from sys_dict_detail t3 where t3.dict_id=251 and t3.value=r1.bis_zhsq_hldz_ssqx) bis_zhsq_hldz_ssqx_name, " +
                "                               r1.bis_zhsq_hldz_xxdz, " +
                "                               r1.bis_zhsq_hldz, " +
                "                               r1.bis_zhsq_jhrxm, " +
                "                               r1.bis_zhsq_jhrdh,  " +
                "                               r1.bis_zhsq_gx bis_zhsq_gx_code, " +
                "                               (select t3.label from sys_dict_detail t3 where t3.dict_id=325 and t3.value=r1.bis_zhsq_gx) bis_zhsq_gx_name, " +
                "                               r1.bis_zhsq_sqyy, " +
                "                               r1.bis_zhsq_ybkh, " +
                "                               r1.bis_zhsq_cbzt, " +
                "                               r1.bis_user_id, " +
                "                               r1.bis_sbgs_id, " +
                "                               r1.bis_pggs_id, " +
                "                               r1.bis_zhsq_grbh, " +
                "                               r1.bis_zhsq_zhlb bis_zhsq_zhlb_code, " +
                "                               (select t3.label from sys_dict_detail t3 where t3.dict_id=1284 and t3.value=r1.bis_zhsq_zhlb) bis_zhsq_zhlb_name, " +
                "                               r1.bis_zhsq_hljg_bm, " +
                "                               r2.bis_gsgl_qymc sbgsmc, " +
                "                               r3.bis_gsgl_qymc hljgmc, " +
                "                               r1.lrsj, " +
                "                               r1.bis_zhsq_sqlb, " +
                "                               r1.bis_zhsq_tjsj, " +
                "                               r1.bis_zhsq_cblx, " +
                "                               r1.bis_zhsq_jbr, " +
                "                               r1.bis_zhsq_jbrxm, " +
                "                               r1.bis_zhsq_cyzt, " +
                "                               to_char(r1.bis_zhsq_jbsj, 'yyyy-mm-dd') bis_zhsq_jbsj " +
                "                          from BIS_ZHSQ_CY r1 " +
                "                          left join bis_gsgl r2 on r1.bis_sbgs_id = r2.bis_gsgl_id " +
                "                          left join bis_gsgl r3 on r1.bis_zhsq_hljg_bm = r3.bis_gsgl_yljgdm " +
                "                           ";

        // 使用预编译防止sql注入
        String sql = "select * from ( " + tempSql + "  ) n where 1=1 ";

        String conditionSql1 = "";
        if(!StringUtils.isEmpty(criteria.getUserId())) {
            conditionSql1 += " and n.bis_zhsq_jbr=:userId ";
        }
        if(criteria.getBisZhsqCyztNull()) {
            conditionSql1 += " and n.bis_zhsq_cyzt is null ";
        } else {
            conditionSql1 += " and n.bis_zhsq_cyzt=:bisZhsqCyzt ";
        }

        if(!StringUtils.isEmpty(criteria.getBisZhsqJbsj())) {
            conditionSql1 += " and n.bis_zhsq_jbsj like :bisZhsqJbsj ";
        }
        if(!StringUtils.isEmpty(criteria.getDdbm())) {
            conditionSql1 += " and n.bis_zhsq_hljg_bm=:ddbm ";
        }
        if(!StringUtils.isEmpty(criteria.getZhlb())) {
            conditionSql1 += " and n.bis_zhsq_zhlb_code=:zhlb ";
        }
        if(!StringUtils.isEmpty(criteria.getXm())) {
            conditionSql1 += " and n.bis_zhsq_snry_xm like :xm ";
        }
        if(!StringUtils.isEmpty(criteria.getSfz())) {
            conditionSql1 += " and n.bis_zhsq_snry_sfz like :sfz ";
        }
        if(!StringUtils.isEmpty(criteria.getBisZhsqCblx())) {
            conditionSql1 += " and n.bis_zhsq_cblx = :cblx ";
        }
        if(!StringUtils.isEmpty(criteria.getSndj())) {
            conditionSql1 += " and n.bis_zhsq_sndj = :sndj ";
        }
        if(!StringUtils.isEmpty(criteria.getSndj())) {
            conditionSql1 += " and n.bis_zhsq_sndj = :sndj ";
        }

        sql += conditionSql1;

        Query query = em.createNativeQuery(sql);
        query.setFirstResult(startEnd[0]);
        query.setMaxResults(startEnd[1] - startEnd[0]);

        if(!StringUtils.isEmpty(criteria.getUserId())) {
            query.setParameter("userId", criteria.getUserId());
        }
        if(!criteria.getBisZhsqCyztNull()) {
            query.setParameter("bisZhsqCyzt", criteria.getBisZhsqCyzt());
        }

        if(!StringUtils.isEmpty(criteria.getBisZhsqJbsj())) {
            query.setParameter("bisZhsqJbsj", criteria.getBisZhsqJbsj() + "%");
        }
        if(!StringUtils.isEmpty(criteria.getDdbm())) {
            query.setParameter("ddbm", criteria.getDdbm());
        }
        if(!StringUtils.isEmpty(criteria.getZhlb())) {
            query.setParameter("zhlb", criteria.getZhlb());
        }
        if(!StringUtils.isEmpty(criteria.getXm())) {
            query.setParameter("xm", "%" + criteria.getXm() + "%");
        }
        if(!StringUtils.isEmpty(criteria.getSfz())) {
            query.setParameter("sfz", "%" + criteria.getSfz() + "%");
        }
        if(!StringUtils.isEmpty(criteria.getBisZhsqCblx())) {
            query.setParameter("cblx", criteria.getBisZhsqCblx());
        }
        if(!StringUtils.isEmpty(criteria.getSndj())) {
            query.setParameter("sndj", criteria.getSndj());
        }
        List result = query.getResultList();
        List<GeneralQueryZhgVo> hljgDspVos = new ArrayList<>();
        for (Object obj : result) {
            Object[] arr = (Object[]) obj;
            GeneralQueryZhgVo vo = new GeneralQueryZhgVo();
            vo.setBisZhsqId(Long.parseLong(arr[0].toString()));
            vo.setBisZhsqSnryXm(arr[3]);
            vo.setBisZhsqSnryXb(arr[4]);
            vo.setBisZhsqSnrySfz(arr[5]);
            vo.setBisZhsqSnryYbssqxCode(arr[6]);
            vo.setBisZhsqSnryYbssqxName(arr[7]);
            vo.setBisZhsqDykkrq(arr[8]);
            vo.setBisZhsqDyjsrq(arr[9]);
            vo.setBisZhsqXsdyq(arr[10]);
            vo.setBisZhsqAdl(arr[11]);
            vo.setBisZhsqZt(arr[12]);
            vo.setBisZhsqAdlPc(arr[13]);
            vo.setBisZhsqSndj(arr[14]);
            vo.setBisZhsqRybh(arr[15]);
            vo.setBisZhsqNl(arr[16]);
            vo.setBisZhsqLxdh(arr[17]);
            vo.setBisZhsqHldzSsqxCode(arr[18]);
            vo.setBisZhsqHldzSsqxName(arr[19]);
            vo.setBisZhsqHldzXxdz(arr[20]);
            vo.setBisZhsqHldz(arr[21]);
            vo.setBisZhsqJhrxm(arr[22]);
            vo.setBisZhsqJhrdh(arr[23]);
            vo.setBisZhsqGxCode(arr[24]);
            vo.setBisZhsqGxName(arr[25]);
            vo.setBisZhsqSqyy(arr[26]);
            vo.setBisZhsqYbkh(arr[27]);
            vo.setBisZhsqCbzt(arr[28]);
            vo.setBisUserId(arr[29]);
            vo.setBisSbgsId(arr[30]);
            vo.setBisPggsId(arr[31]);
            vo.setBisZhsqGrbh(arr[32]);
            vo.setBisZhsqZhlbCode(arr[33]);
            vo.setBisZhsqZhlbName(arr[34]);
            vo.setBisZhsqHljgBm(arr[35]);
            vo.setBisSbgsQymc(arr[36]);
            vo.setBisHljgQymc(arr[37]);
            vo.setLrsj(arr[38]);
            vo.setBisZhsqSqlb(arr[39]);
            vo.setBisZhsqTjsj(arr[40]);
            vo.setBisZhsqCblx(arr[41]);
            vo.setBisZhsqJbr(arr[42]);
            vo.setBisZhsqJbrxm(arr[43]);
            vo.setBisZhsqCyzt(arr[44]);
            vo.setBisZhsqJbsj(arr[45]);
            hljgDspVos.add(vo);
        }
        String countSql = "select count(1) from ( " + tempSql + "  ) n where 1=1 ";
        String conditionSql2 = "";

        if(!StringUtils.isEmpty(criteria.getUserId())) {
            conditionSql2 += " and n.bis_zhsq_jbr=:userId ";
        }
        if(criteria.getBisZhsqCyztNull()) {
            conditionSql2 += " and n.bis_zhsq_cyzt is null ";
        } else {
            conditionSql2 += " and n.bis_zhsq_cyzt=:bisZhsqCyzt ";
        }

        if(!StringUtils.isEmpty(criteria.getBisZhsqJbsj())) {
            conditionSql2 += " and n.bis_zhsq_jbsj like :bisZhsqJbsj ";
        }
        if(!StringUtils.isEmpty(criteria.getDdbm())) {
            conditionSql2 += " and n.bis_zhsq_hljg_bm=:ddbm ";
        }
        if(!StringUtils.isEmpty(criteria.getZhlb())) {
            conditionSql2 += " and n.bis_zhsq_zhlb_code=:zhlb ";
        }
        if(!StringUtils.isEmpty(criteria.getXm())) {
            conditionSql2 += " and n.bis_zhsq_snry_xm like :xm ";
        }
        if(!StringUtils.isEmpty(criteria.getSfz())) {
            conditionSql2 += " and n.bis_zhsq_snry_sfz like :sfz ";
        }
        if(!StringUtils.isEmpty(criteria.getBisZhsqCblx())) {
            conditionSql2 += " and n.bis_zhsq_cblx = :cblx ";
        }
        if(!StringUtils.isEmpty(criteria.getSndj())) {
            conditionSql2 += " and n.bis_zhsq_sndj = :sndj ";
        }
        countSql += conditionSql2;
        Query queryCount = em.createNativeQuery(countSql);

        if(!StringUtils.isEmpty(criteria.getUserId())) {
            queryCount.setParameter("userId", criteria.getUserId());
        }
        if(!criteria.getBisZhsqCyztNull()) {
            queryCount.setParameter("bisZhsqCyzt", criteria.getBisZhsqCyzt());
        }

        if(!StringUtils.isEmpty(criteria.getBisZhsqJbsj())) {
            queryCount.setParameter("bisZhsqJbsj", criteria.getBisZhsqJbsj() + "%");
        }
        if(!StringUtils.isEmpty(criteria.getDdbm())) {
            queryCount.setParameter("ddbm", criteria.getDdbm());
        }
        if(!StringUtils.isEmpty(criteria.getZhlb())) {
            queryCount.setParameter("zhlb", criteria.getZhlb());
        }
        if(!StringUtils.isEmpty(criteria.getXm())) {
            queryCount.setParameter("xm", "%" + criteria.getXm() + "%");
        }
        if(!StringUtils.isEmpty(criteria.getSfz())) {
            queryCount.setParameter("sfz", "%" + criteria.getSfz() + "%");
        }
        if(!StringUtils.isEmpty(criteria.getBisZhsqCblx())) {
            queryCount.setParameter("cblx", criteria.getBisZhsqCblx());
        }
        if(!StringUtils.isEmpty(criteria.getSndj())) {
            queryCount.setParameter("sndj", criteria.getSndj());
        }
        Object totalElements = queryCount.getSingleResult();
        log.info("totalElements {} ", totalElements);
        Map<String,Object> m = PageUtil.toPage(hljgDspVos, totalElements);
        return m;
    }

    @Override
    public void queryZhsqCyDown(ZhsqCyQueryCriteria criteria, HttpServletResponse response) throws IOException {

        String tempSql =  "select r1.bis_zhsq_id, " +
                "                               r1.bis_zhsq_hljg_id, " +
                "                               r1.bis_zhsq_snry_id, " +
                "                               r1.bis_zhsq_snry_xm, " +
                "                               r1.bis_zhsq_snry_xb, " +
                "                               r1.bis_zhsq_snry_sfz, " +
                "                               r1.bis_zhsq_snry_ybssqx bis_zhsq_snry_ybssqx_code, " +
                "                               (select t3.label from sys_dict_detail t3 where t3.dict_id=1001 and t3.value=r1.bis_zhsq_snry_ybssqx) bis_zhsq_snry_ybssqx_name, " +
                "                               to_char(r1.bis_zhsq_dykkrq, 'yyyy-mm-dd') bis_zhsq_dykkrq, " +
                "                               to_char(r1.bis_zhsq_dyjsrq, 'yyyy-mm-dd') bis_zhsq_dyjsrq, " +
                "                               r1.bis_zhsq_xsdyq, " +
                "                               r1.bis_zhsq_adl, " +
                "                               r1.bis_zhsq_zt, " +
                "                               r1.bis_zhsq_adl_pc, " +
                "                               r1.bis_zhsq_sndj, " +
                "                               r1.bis_zhsq_rybh, " +
                "                               r1.bis_zhsq_nl, " +
                "                               r1.bis_zhsq_lxdh, " +
                "                               r1.bis_zhsq_hldz_ssqx bis_zhsq_hldz_ssqx_code, " +
                "                               (select t3.label from sys_dict_detail t3 where t3.dict_id=251 and t3.value=r1.bis_zhsq_hldz_ssqx) bis_zhsq_hldz_ssqx_name, " +
                "                               r1.bis_zhsq_hldz_xxdz, " +
                "                               r1.bis_zhsq_hldz, " +
                "                               r1.bis_zhsq_jhrxm, " +
                "                               r1.bis_zhsq_jhrdh,  " +
                "                               r1.bis_zhsq_gx bis_zhsq_gx_code, " +
                "                               (select t3.label from sys_dict_detail t3 where t3.dict_id=325 and t3.value=r1.bis_zhsq_gx) bis_zhsq_gx_name, " +
                "                               r1.bis_zhsq_sqyy, " +
                "                               r1.bis_zhsq_ybkh, " +
                "                               r1.bis_zhsq_cbzt, " +
                "                               r1.bis_user_id, " +
                "                               r1.bis_sbgs_id, " +
                "                               r1.bis_pggs_id, " +
                "                               r1.bis_zhsq_grbh, " +
                "                               r1.bis_zhsq_zhlb bis_zhsq_zhlb_code, " +
                "                               (select t3.label from sys_dict_detail t3 where t3.dict_id=1284 and t3.value=r1.bis_zhsq_zhlb) bis_zhsq_zhlb_name, " +
                "                               r1.bis_zhsq_hljg_bm, " +
                "                               r2.bis_gsgl_qymc sbgsmc, " +
                "                               r3.bis_gsgl_qymc hljgmc, " +
                "                               r1.lrsj, " +
                "                               r1.bis_zhsq_sqlb, " +
                "                               r1.bis_zhsq_tjsj, " +
                "                               r1.bis_zhsq_cblx, " +
                "                               r1.bis_zhsq_jbr, " +
                "                               r1.bis_zhsq_jbrxm, " +
                "                               r1.bis_zhsq_cyzt, " +
                "                               to_char(r1.bis_zhsq_jbsj, 'yyyy-mm-dd') bis_zhsq_jbsj, " +
                "                               to_char(r1.lrsj, 'yyyy-mm-dd hh24:mi:ss') " +
                "                          from BIS_ZHSQ_CY r1 " +
                "                          left join bis_gsgl r2 on r1.bis_sbgs_id = r2.bis_gsgl_id " +
                "                          left join bis_gsgl r3 on r1.bis_zhsq_hljg_bm = r3.bis_gsgl_yljgdm " +
                "                           ";

        // 使用预编译防止sql注入
        String sql = "select * from ( " + tempSql + "  ) n where 1=1 ";

        String conditionSql1 = "";
        if(!StringUtils.isEmpty(criteria.getUserId())) {
            conditionSql1 += " and n.bis_zhsq_jbr=:userId ";
        }
        if(criteria.getBisZhsqCyztNull()) {
            conditionSql1 += " and n.bis_zhsq_cyzt is null ";
        } else {
            conditionSql1 += " and n.bis_zhsq_cyzt=:bisZhsqCyzt ";
        }

        if(!StringUtils.isEmpty(criteria.getBisZhsqJbsj())) {
            conditionSql1 += " and n.bis_zhsq_jbsj like :bisZhsqJbsj ";
        }
        if(!StringUtils.isEmpty(criteria.getDdbm())) {
            conditionSql1 += " and n.bis_zhsq_hljg_bm=:ddbm ";
        }
        if(!StringUtils.isEmpty(criteria.getZhlb())) {
            conditionSql1 += " and n.bis_zhsq_zhlb_code=:zhlb ";
        }
        if(!StringUtils.isEmpty(criteria.getXm())) {
            conditionSql1 += " and n.bis_zhsq_snry_xm like :xm ";
        }
        if(!StringUtils.isEmpty(criteria.getSfz())) {
            conditionSql1 += " and n.bis_zhsq_snry_sfz like :sfz ";
        }
        if(!StringUtils.isEmpty(criteria.getBisZhsqCblx())) {
            conditionSql1 += " and n.bis_zhsq_cblx = :cblx ";
        }
        if(!StringUtils.isEmpty(criteria.getSndj())) {
            conditionSql1 += " and n.bis_zhsq_sndj = :sndj ";
        }
        if(!StringUtils.isEmpty(criteria.getSndj())) {
            conditionSql1 += " and n.bis_zhsq_sndj = :sndj ";
        }

        sql += conditionSql1;

        Query query = em.createNativeQuery(sql);

        if(!StringUtils.isEmpty(criteria.getUserId())) {
            query.setParameter("userId", criteria.getUserId());
        }
        if(!criteria.getBisZhsqCyztNull()) {
            query.setParameter("bisZhsqCyzt", criteria.getBisZhsqCyzt());
        }

        if(!StringUtils.isEmpty(criteria.getBisZhsqJbsj())) {
            query.setParameter("bisZhsqJbsj", criteria.getBisZhsqJbsj() + "%");
        }
        if(!StringUtils.isEmpty(criteria.getDdbm())) {
            query.setParameter("ddbm", criteria.getDdbm());
        }
        if(!StringUtils.isEmpty(criteria.getZhlb())) {
            query.setParameter("zhlb", criteria.getZhlb());
        }
        if(!StringUtils.isEmpty(criteria.getXm())) {
            query.setParameter("xm", "%" + criteria.getXm() + "%");
        }
        if(!StringUtils.isEmpty(criteria.getSfz())) {
            query.setParameter("sfz", "%" + criteria.getSfz() + "%");
        }
        if(!StringUtils.isEmpty(criteria.getBisZhsqCblx())) {
            query.setParameter("cblx", criteria.getBisZhsqCblx());
        }
        if(!StringUtils.isEmpty(criteria.getSndj())) {
            query.setParameter("sndj", criteria.getSndj());
        }
        List result = query.getResultList();

        List<Map<String, Object>> list = new ArrayList<>();
        int cnt = 0;
        for (Object obj : result) {

            cnt++;
            Object[] arr = (Object[]) obj;
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("序号", cnt);
            map.put("申请时间", arr[46]);
            map.put("医保卡号", arr[27]);
            map.put("姓名", arr[3]);
            map.put("性别", arr[4]);
            map.put("身份证", arr[5]);
            map.put("参保类型", arr[41]);
            map.put("年龄", arr[16]);
            map.put("待遇开始日期", arr[8]);
            map.put("待遇结束日期", arr[9]);
            map.put("失能等级", arr[14]);
            map.put("ADL评分", arr[11]);
            map.put("统筹区", arr[7]);
            map.put("商保机构", arr[36]);
            map.put("定点编码", arr[35]);
            map.put("护理机构", arr[37]);
            map.put("经办人", arr[43]);
            map.put("经办日期", arr[45]);
            map.put("经办状态", "60".equals(arr[44]) ? "通过" : "61".equals(arr[44]) ? "存疑" : "");
            list.add(map);


        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public Map<String,Object> queryAll(ZhsqCyQueryCriteria criteria, Pageable pageable){
        Page<ZhsqCy> page = zhsqCyRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(zhsqCyMapper::toDto));
    }

    @Override
    public List<ZhsqCyDto> queryAll(ZhsqCyQueryCriteria criteria){
        return zhsqCyMapper.toDto(zhsqCyRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public ZhsqCyDto findById(Long bisZhsqId) {
        ZhsqCy zhsqCy = zhsqCyRepository.findById(bisZhsqId).orElseGet(ZhsqCy::new);
        ValidationUtil.isNull(zhsqCy.getBisZhsqId(),"ZhsqCy","bisZhsqId",bisZhsqId);
        return zhsqCyMapper.toDto(zhsqCy);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ZhsqCyDto create(ZhsqCy resources) {
        Snowflake snowflake = IdUtil.createSnowflake(1, 1);
        resources.setBisZhsqId(snowflake.nextId()); 
        return zhsqCyMapper.toDto(zhsqCyRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(ZhsqCy resources) {
        ZhsqCy zhsqCy = zhsqCyRepository.findById(resources.getBisZhsqId()).orElseGet(ZhsqCy::new);
        ValidationUtil.isNull( zhsqCy.getBisZhsqId(),"ZhsqCy","id",resources.getBisZhsqId());
        zhsqCy.copy(resources);
        zhsqCyRepository.save(zhsqCy);
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long bisZhsqId : ids) {
            zhsqCyRepository.deleteById(bisZhsqId);
        }
    }

    @Override
    public void download(List<ZhsqCyDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (ZhsqCyDto zhsqCy : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}